﻿<?php
class GlobalAction extends Action
{
	protected $company_id,$company_code,$company_name,$company_theme;
	public function _initialize(){ 
	$id = (int)$_REQUEST['company_id'];
	if(isset($_COOKIE['c'.$id]['company_id']))
	{
		$this->company_id = $_COOKIE['c'.$id]['company_id'];
		$this->company_code = $_COOKIE['c'.$id]['company_code'];
		$this->company_name = $_COOKIE['c'.$id]['company_name'];
		$this->company_theme = $_COOKIE['c'.$id]['company_theme'];
    }
	else
	{
		$list = M('company')->where('id='.$id)->find();
		if($list)
		{
		$this->company_id = $list['id'];
		$this->company_code = $list['company_code'];
		$this->company_name = $list['company_name'];
		$this->company_theme = $list['company_theme'];
		
		 $_COOKIE['c'.$id]['company_id'] = $this->company_id ;
		 $_COOKIE['c'.$id]['company_code'] = $this->company_code ;
		 $_COOKIE['c'.$id]['company_name'] = $this->company_name;
		 $_COOKIE['c'.$id]['company_theme'] = $this->company_theme;
		}
		else
		{
			//echo '该公司不存在或已经删除';
			self::_message('error','该公司不存在或已经删除！');
			exit();
		}
	}
	C('DEFAULT_THEME',$this->company_theme);
	} 
   
//操作v信息提示 $type : success,error errorurl
protected function _message($type = 'success',$content = '更新成功',$jumpUrl = __URL__,$time = 1,$ajax = false){
	$jumpUrl = empty($jumpUrl) ?__URL__ : $jumpUrl ;
	switch ($type){
		case 'success':
			$this->assign('jumpUrl',$jumpUrl);
			$this->assign('waitSecond',$time);
			$this->success($content,$ajax);
			break;
		case 'error':
			$this->assign('jumpUrl','javascript:history.back(-1);');
			$this->assign('waitSecond',$time);
			$this->assign('error',$content);
			$this->error($content,$ajax);
			break;
		case 'errorUrl':
			$this->assign('jumpUrl',$jumpUrl);
			$this->assign('waitSecond',$time);
			$this->assign('error',$content);
			$this->error($content,$ajax);
			break;
		default:
			die('error type');
			break;
	}
}

//获取指定表中符合条件的一条记录的字段
protected function _get_field($table,$condition,$field)
{
	$list_temp = M($table)->where($condition)->getField($field);
	//dump($list_temp);
	return $list_temp;	
}
//公共左边部分
protected function pub_left(){
$list_info = self::company_info();
$this->assign('list_info',$list_info);

$list_pclass = self::company_product_class();
$this->assign('list_pclass',$list_pclass);

$list_tongji = self::company_tongji();
$this->assign('list_tongji',$list_tongji);	
}
//公司信息
private function company_info(){
$list = M('company')->where('id='.$this->company_id)->find();
return $list;	
}

//公司产品分类
private function company_product_class(){
$list = M('product_class')->where('company_id='.$this->company_id)->select();
return $list;	
}

//公司简单统计
private function company_tongji(){
$list = array();
//产品数量
$list[] = self::_get_field('products','company_id='.$this->company_id,'count(id) as t_num');
$list[] = self::_get_field('products','w_tuijian=1 and company_id='.$this->company_id,'count(id) as t_num');
$list[] = self::_get_field('sell_product','sell_fangshi=1 and company_id='.$this->company_id,'count(id) as t_num');
return $list;
}

}
?>